<style type="text/css">
#menuTranslationGrid tbody td {
	height: 25px;
}
#menuTranslationGrid tbody td > div {
	height: 24px;
}
</style>
<div id="replaceMenuText" class="pageContent" style="display: none;">
	<div class="pageFormContent">
		<dl>
			<dt>查找目标：</dt>
			<dd>
				<input id="regexp" class='textInput required' />
			</dd>
		</dl>
		<dl class="nowrap">
			<dt>替换为：</dt>
			<dd>
				<input id="replaceValue" class="textInput" />
			</dd>
		</dl>
		<div class="formBar"><!-- caseSensitive.C -->
			<label style="width: 91px; float: left;"><input id="caseSensitive" type="checkbox" checked="checked" />区分大小写</label>
			<ul>
				<li><div class="buttonActive">
						<div class="buttonContent">
							<button type="button" id="replace" >替换</button>
						</div>
					</div></li>
				<li>
					<div class="buttonActiveGrey">
						<div class="buttonContent">
							<button type="button" class="close">取消</button>
						</div>
					</div>
				</li>
			</ul>
		</div>
	</div>
</div>
<div id="menuTranslationGrid" style="margin-top: -1px;" class="pageContent"></div>
<script>
(function(){
	var $page = DWZ.context(), $grid = $('#menuTranslationGrid', $page), $dialogContext = $('#replaceMenuText', $page), checkValidData = function() {
		if ($('tbody>tr:first .noData', $grid).length > 0) {
			alertMsg.error("没有可操作的有效数据！");
			return false;
		}
		return true;
	};
	$grid.jGrid({
		layoutH: 60,
        base: hostUrl + '/menuTranslation/',
        url: 'getMenuList',
        pageable: false,
        columns: [
            {field: '_index', text: '<@label key="serialNumber.C">序号</@label>', width: 20},
            {field: 'menuCode', text: '<@label key="menuCode.C">菜单编号</@label>', width: 200},
            {field: 'menuText', text: '源语言描述（${sourceLanguageText!"默认"}）', width: 200},
            {field: 'targetMenuText', type: 'textInput', text: '目标语言描述（${targetLanguageText}）', width: 200}
        ],
        getQueryData: function() {
        	return {
        		navTabId: "${navTabId!''}",
				menuCode: "${menuCode!''}",
    			sourceLanguage: "${sourceLanguage!''}",
    			targetLanguage: "${targetLanguage!''}"
        	};
        },
        gridbars: [
            {icon: 'edit', text: '从源资源复制', title: '从源资源复制', target: 'action', action: function() {
            	if (!checkValidData()) {
            		return false;
            	}
                var $tbody = $('tbody', $grid);
                $('>tr', $tbody).each(function() {
                	var $tr = $(this);
                	$('>td:eq(3) input.textInput', $tr).val($('>td:eq(2)>div', $tr).text());
                });
                return false;
            }, menuCode: "defectType_edit"},
            {icon: 'delete', text: '清除目标资源', title: "清除目标资源", target: 'action', action: function() {
            	if (!checkValidData()) {
            		return false;
            	}
            	var $tbody = $('tbody', $grid);
            	$('>tr>td:nth-child(4) input.textInput', $tbody).val('');
            	return false;
            }, menuCode: "defectType_edit"},
            {icon: 'test', text: '替换目标资源', title: "替换目标资源", target: 'action', action: function() {
            	if (!checkValidData()) {
            		return false;
            	}
            	$.pdialog.open('', "replaceMenuText", "替换目标资源", {height: 150, width: 300, max: false, mask: true, resizable: false, maxable: false, minable: false, callback: function($content){
            		var $form = $('<form/>');
            		$dialogContext.clone().removeAttr('id').show().appendTo($form.appendTo($content));
                    $('button.close', $content).on('click', function(e){
                        $.pdialog.close($(this).parents('.dialog:first'));
                        var event = e || window.event;
                        event.preventDefault();
                    });
                    $('dl', $content).css({width: 285});
                    $('#replace', $content).on('click', function(){
                    	if (!$form.valid()) {
                    		$('span.error', $content).css({marginLeft: 3});
                    		return false;
                    	}
                    	var isCaseSensitive = $('#caseSensitive', $form)[0].checked;
                    	console.log(isCaseSensitive)
                    	var regexp = new RegExp($('#regexp', $form).val(), isCaseSensitive ? 'g' : 'gi'), replaceValue = $('#replaceValue', $form).val(), replacedQuantity = 0;
                    	$('tbody>tr>td:nth-child(4) input.textInput', $grid).each(function(){
                    		if (regexp.test(this.value)) {
                    			replacedQuantity++;
                    			$(this).addClass('focus');
	                    		this.value = this.value.replace(regexp, replaceValue);
                    		}
                    	});
                    	if (replacedQuantity > 0) {
	                    	$.pdialog.closeCurrent();
                    	} else {
                    		alertMsg.error("没有任何匹配的描述！");
                    	}
                    });
                }});
            	return false;
            }, menuCode: "defectType_view"},
			{text : '返回', title : '确认返回吗？', type : 'button', render : function($li) {
					$li.css({
						float : 'right',
						'margin-right' : '10px'
					});
					$li.prev('li').css({
						'border-width' : '0px'
					});
					$('>div', $li).removeClass('buttonActive').addClass('buttonActiveGrey');
				},
				target : 'confirmTodo',
				action : function() {
					navTab.openTab("${navTabId!''}", hostUrl + '/menuTranslation/indexPage', {
						data : {
							navTabId : "${navTabId!''}",
							menuCode: "${menuCode!''}",
							sourceLanguage: "${sourceLanguage!''}",
							targetLanguage: "${targetLanguage!''}"
						},
						title : '菜单翻译',
						fresh : true,
						external : false
					});
				}
			},
			{type : 'button', text : '<@label key="save.B">保存</@label>', title : '', target : 'confirmTodo',
				render : function($li) {
					$li.css({
						float : 'right',
						'margin-right' : '10px'
					});
					$li.prev('li').css({
						'border-width' : '0px'
					});
				},
				action : function() {
					if (!checkValidData()) {
	            		return false;
	            	}
					var menus = new Array();
					$('tbody>tr', $grid).each(function(){
						var $tr = $(this);
						var menu = {
							menuCode: $tr.data('menuCode'),
							targetMenuText: $('>td:eq(3) input.textInput', $tr).val()
						}
						menus.push(menu);
					});
					// 保存操作
					DWZ.ajax({
						url : hostUrl + '/menuTranslation/saveMenuList',
						data : {
							menus: JSON.stringify(menus),
							targetLanguage: "${targetLanguage!''}"
						},
						success : function() {
							
						}
					});
				}
			} ]
		});
	})();
</script>